package org.algorithm.Sort;

public class QuickSort1 {

    public static void main(String[] args) {
        int [] arr = {1,5,3,2,8,6,10};

        QuickSortBy3Ways(arr,0,arr.length-1);

        for (int num :arr) {
            System.out.println(num);
        }
    }


    public static void QuickSortBy3Ways(int [] arr,int start,int end){

        if (start>=end){
            return;
        }

        int piv = arr[start];
        int left = start;
        int right = end;

        int curr = left+1;

        while (curr<=right){
            if (arr[curr]<piv){
                swap(arr,curr,left);
                left++;
                curr++;
            }else if (arr[curr]>piv){
                swap(arr,curr,right);
                right--;
            }else {
                curr++;
            }
        }

        QuickSortBy3Ways(arr,start,left-1);
        QuickSortBy3Ways(arr,right+1,end);
    }

    public static void swap(int [] arr,int index1,int index2){
            int temp = arr[index1];
            arr[index1]=arr[index2];
            arr[index2]=temp;
    }

}
